package com.examples.ry.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class DrawText {
	static final String string = "&Hello,\tWorld\nfrom\tOttawa";

	public static void main(String[] args) {
		final Display display = new Display();
		final Color white = display.getSystemColor(SWT.COLOR_WHITE);
		final Color gray = display.getSystemColor(SWT.COLOR_GRAY);
		final Color black = display.getSystemColor(SWT.COLOR_BLACK);
		final Shell shell = new Shell(display);
		shell.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				Rectangle bounds = shell.getClientArea();
				int top = bounds.y;
				int left = bounds.x;
				GC gc = event.gc;
				//int yAdvance = bounds.height / 10;
				int yAdvance = gc.getFontMetrics().getHeight(); //improved way
				gc.setBackground(gray);
				gc.fillRectangle(bounds);
				gc.setBackground(white);
				gc.setForeground(black);
				gc.drawString(string, left, top, false);
				gc.drawString(string, left, top + yAdvance, true);
				gc.drawText(string, left, top + (2 * yAdvance), false);
				gc.drawText(string, left, top + (4 * yAdvance), true);
				gc.setForeground(gray);
				gc.drawText(string, left, top + (6 * yAdvance),
						SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC);
				gc.drawText(string, left, top + (8 * yAdvance),
						SWT.DRAW_MNEMONIC);
			}
		});
		shell.setText("Draw Text");
		shell.setSize(400, 400);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}